﻿using System;
using System.Web.Hosting;
using System.Web.Mvc;
using UIShell.OSGi;

namespace UIShell.OSGI.Mvc
{
    public static class UrlHelperExtensions
    {
        public static string BundleContent(this UrlHelper helper, string location)
        {
            var symbolicName = helper.RequestContext.RouteData.GetPluginSymbolicName();
            if (string.IsNullOrEmpty(symbolicName)) return helper.Content(location);

            var bundle = BundleRuntime.Instance.Framework.GetBundleBySymbolicName(symbolicName);
            if (bundle == null) return helper.Content(location);

            var bundleLocation = @"~\" + bundle.Location.Replace(HostingEnvironment.ApplicationPhysicalPath, String.Empty);
            location = location.Replace("~", bundleLocation);
            return helper.Content(location);
        }
    }
}
